﻿using System;

namespace Apex.DomainCommons.Models
{
    /// <summary>
    /// 表示聚合根实体的基类，实现了聚合根、软删除、创建时间、删除时间和修改时间的接口
    /// </summary>
    public record AggregateRootEntity : BaseEntity, IAggregateRoot, ISoftDelete, IHasCreationTime, IHasDeletionTime, IHasModificationTime
    {
        /// <summary>
        /// 获取一个值，指示实体是否已被软删除
        /// </summary>
        public bool IsDeleted { get; private set; }

        /// <summary>
        /// 获取实体的创建时间
        /// </summary>
        public DateTime CreationTime { get; private set; } = DateTime.Now;

        /// <summary>
        /// 获取或设置实体的删除时间
        /// </summary>
        public DateTime? DeletionTime { get; private set; }

        /// <summary>
        /// 获取或设置实体的最后修改时间
        /// </summary>
        public DateTime? LastModificationTime { get; private set; }

        /// <summary>
        /// 将实体标记为已被软删除
        /// </summary>
        /// <remarks>
        /// 调用此方法将设置<see cref="IsDeleted"/>属性为<see langword="true"/>，表示实体已被软删除。
        /// 同时设置<see cref="DeletionTime"/>属性为当前时间，表示实体被删除的时间。
        /// </remarks>
        public virtual void SoftDelete()
        {
            this.IsDeleted = true;
            this.DeletionTime = DateTime.Now;
        }

        /// <summary>
        /// 通知实体已被修改
        /// </summary>
        /// <remarks>
        /// 调用此方法将设置<see cref="LastModificationTime"/>属性为当前时间，表示实体的最后修改时间。
        /// </remarks>
        public void NotifyModified()
        {
            this.LastModificationTime = DateTime.Now;
        }
    }
}
